package com.tarena.lbs.marketing.web.controller;

import com.tarena.lbs.marketing.web.scheduled.ActivityScheduled;
import com.tarena.lbs.marketing.web.service.MarketingActivityService;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.marketing.converter.MarketingActivityConverter;
import com.tarena.lbs.pojo.marketing.bo.MarketingActivityBO;
import com.tarena.lbs.pojo.marketing.param.MarketingActivityParam;
import com.tarena.lbs.pojo.marketing.param.UpdateParam;
import com.tarena.lbs.pojo.marketing.query.MarketingActivityQuery;
import com.tarena.lbs.pojo.marketing.vo.MarketingActivityVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
@RequestMapping("/admin/marketing/marketingActivity/info")
@Slf4j
@Api(tags = "活动管理")
public class MarketingActivityController {

    @Resource
    private MarketingActivityService marketingActivityService;

    @Resource
    private MarketingActivityConverter marketingActivityConverter;

    @Resource
    private ActivityScheduled activityScheduled;

    @PostMapping("/save")
    @ApiOperation("新增活动")
    public Result<Void> insert(@Valid @RequestBody MarketingActivityParam param) throws BusinessException {
        marketingActivityService.insert(param);
        return Result.success();
    }

    @PostMapping("/list")
    @ApiOperation("查询活动列表")
    public Result<PageResult<MarketingActivityVO>> query(@Valid @RequestBody MarketingActivityQuery query){
        BasePageList<MarketingActivityBO> activityBos = marketingActivityService.list(query);
        PageResult<MarketingActivityVO> page = marketingActivityConverter.assemblePageVOS(activityBos,query);
        return new Result<>(page);
    }

    @PostMapping("/update")
    @ApiOperation("更新活动")
    public Result<Void> update(@Valid @RequestBody MarketingActivityParam param) throws BusinessException {
        marketingActivityService.update(param);
        return Result.success();
    }

    @PostMapping("/updateStatus")
    @ApiOperation("更新活动状态")
    public Result<Void> updateStatus(@Valid @RequestBody UpdateParam updateParam){
        activityScheduled.updateActivityStatus();
        return Result.success();
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("活动详情")
    public Result<MarketingActivityVO> getDetail(@PathVariable("id") Integer activityId){
        MarketingActivityBO marketingActivityBO = marketingActivityService.getDetail(activityId);
        return new Result<>(marketingActivityConverter.bo2vo(marketingActivityBO));
    }
}
